// 2013-9-3下午04:25:39  net.betou.trade.front.action.ContentAct.java

package net.betou.trade.front.action;

import static net.betou.common.page.SimplePage.cpn;
import static net.betou.common.page.SimplePage.cpnPageSize;
import static net.betou.web.Constants.TPLDIR_CONTENT;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.common.page.Pagination;
import net.betou.common.security.encoder.PwdEncoder;
import net.betou.common.web.RequestUtils;
import net.betou.trade.entity.main.Content;
import net.betou.trade.entity.main.User;
import net.betou.trade.main.manager.ContentMng;
import net.betou.trade.main.manager.UserMng;
import net.betou.trade.web.Messages;
import net.betou.trade.web.WebErrors;
import net.betou.web.FrontUtils;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 内容Action
 * @author 叶装装
 */
@Controller
public class ContentAct{

	public static final String CONTENT_TPL = "内容预览";
	public static final String CONTENT_LIST_TPL = "内容列表";
	
	
	@RequestMapping( "/content/view.jhtml" )
	public String toView( Integer id, HttpServletRequest request, HttpServletResponse response, ModelMap model ) {

		WebErrors errors = WebErrors.create( );

		if( id == null || id == 0 ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors.showErrorPage( model );
		}

		Content content = contentMng.findById( id );

		model.addAttribute( "content", content );


		FrontUtils.frontData( request, model );

		return FrontUtils.getTplPath( TPLDIR_CONTENT, CONTENT_TPL );
	}
	
	@RequestMapping( "/content/list.jhtml" )
	public String toList( String username, String password, Integer pageSize, Integer pageNo, HttpServletRequest request, HttpServletResponse response, ModelMap model  ){
		FrontUtils.frontData( request, model );
		
		username = RequestUtils.parseParamUsingUTF8( username );
		
		WebErrors errors = validateList( username, password );
		// 返回错误消息
		if( errors.hasErrors( ) ){
			return FrontUtils.showMessage( request, model, errors.getErrors( ) );
		}
		
		FrontUtils.frontData( request, model );
		
		User user = userMng.findByUsername( username );
		
		pageSize = cpnPageSize( pageSize );
		pageNo = cpn( pageNo );
		
		Pagination pagination = contentMng.getPage(  user, pageNo, pageSize );
		
		model.addAttribute( "pagination", pagination );
		//model.addAttribute( "notice", noticeMng.getLastNotice( ) );
		model.addAttribute( "hrefFormer", "/content/list.jhtml?username=" + username + "&password=" + password );
		
		return FrontUtils.getTplPath( TPLDIR_CONTENT, CONTENT_LIST_TPL );
	}
	
	private WebErrors validateList( String username, String password ) {

		WebErrors errors = WebErrors.create( );

		if( StringUtils.isBlank( username ) || StringUtils.isBlank( password ) ){
			errors.addError( Messages._403 );
			return errors;
		}

		User user = userMng.findByUsername( username );

		if( user == null ){
			errors.addError( Messages._403 );
			return errors;
		}

		// 密码不正确
		if( !pwdEncoder.isPasswordValid( user.getPassword( ), password, user.getSalt( ) ) ){
			errors.addError( Messages._403 );
			return errors;
		}
		
		return errors;

	}
	
	@Autowired
	private ContentMng contentMng;
	
	@Autowired
	private UserMng userMng;
	
	@Autowired
	private PwdEncoder pwdEncoder;
}
